package com.road.dao.imp;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import com.road.dao.IPraiseDao;
import com.road.model.Praise;
import com.road.utils.DruidPlugin;

public class PraiseDaoImp implements IPraiseDao{
//	两种连接池切换
//	QueryRunner qr=new QueryRunner(DataSourceUtils.getDatasSource());
	QueryRunner qr=new QueryRunner(DruidPlugin.getDataSource());

	@Override
	public int updateClickPraise(String phone,String news_id) throws SQLException  {
		// TODO Auto-generated method stub
		 String sql="insert into d_news_praise  (phone,news_id,praise_time) values(?,?,now())";
		 Object[] params= {phone,news_id};
		return  qr.update(sql, params);
	}

	@Override
	public List<Praise> getPraisePeopleListByNewsId(String news_id) throws SQLException {
		// TODO Auto-generated method stub
		String sql="SELECT a.*,b.head_icon,b.user_name from d_news_praise as a,auther_user as b where a.phone=b.phone and a.news_id="+news_id;
	 System.out.println(sql);
		return  qr.query(sql, new BeanListHandler<Praise>(Praise.class));
	}

	@Override
	public boolean getPraiseByNewsIdAndPhone(String phone, String new_id) throws SQLException {
		// TODO Auto-generated method stub
		String sql="SELECT news_id from d_news_praise where phone='"+phone+"' and news_id="+new_id;
		Object result = qr.query(sql, new ScalarHandler());
		return result==null?false:true;
	}

	@Override
	public boolean delPraiseByNewsIdAndPhone(String phone, String new_id) throws SQLException {
		// TODO Auto-generated method stub
		int i = qr.update("delete from d_news_praise where phone='"+phone+"' and news_id="+new_id);
		return i>0;
	}
	
	
}
